package com.liveinstars.im.endpoint;

import com.liveinstars.im.core.service.CandyService;
import com.liveinstars.api.im.CandyApi;
import com.liveinstars.api.im.vo.CandyBoxVO;
import com.liveinstars.im.common.exception.IMRuntimeException;
import com.onepiece.shipelves.common.Result;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.exception.SystemException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zhouze
 * @date: 2020/3/20
 */
@RestController
public class CandyEndpoint implements CandyApi {

    private static final Logger logger = LoggerFactory.getLogger(CandyEndpoint.class);

    @Autowired
    private CandyService candyService;

    @Override
    public Result<CandyBoxVO> createCandyBox(@RequestHeader("uid") Long userId, @RequestBody CandyBoxVO vo) {
        Result<CandyBoxVO> result = new Result<>();
        try {
            CandyBoxVO candyBox = candyService.createCandyBox(userId, vo);
            result.setData(candyBox);
        } catch (Exception e) {
            if (e instanceof IMRuntimeException) {
                result = ((IMRuntimeException) e).createResult();
            } else if (e instanceof SystemException){
                result.setCode(((SystemException) e).getCode());
                result.setMessage(e.getMessage());
            } else {
                logger.error("createCandyBox UN_KNOW_ERROR : {}", e.getMessage(), e);
                result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
            }
        }
        return result;
    }

    @Override
    public Result<CandyBoxVO> getCandy(@RequestHeader("uid") Long userId, @RequestParam("candy_box_id") Long candyBoxId) {
        Result<CandyBoxVO> result = new Result<>();
        try {
            CandyBoxVO candyBox = candyService.getCandy(userId, candyBoxId);
            result.setData(candyBox);
        } catch (Exception e) {
            if (e instanceof IMRuntimeException) {
                result = ((IMRuntimeException) e).createResult();
            } else {
                logger.error("getCandy UN_KNOW_ERROR : {}", e.getMessage(), e);
                result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
            }
        }
        return result;
    }

    @Override
    public Result<CandyBoxVO> getCandyBoxDetail(@RequestParam("candy_box_id") Long candyBoxId) {
        Result<CandyBoxVO> result = new Result<>();
        try {
            CandyBoxVO candybox = candyService.getCandyBoxDetail(candyBoxId);
            result.setData(candybox);
        } catch (Exception e) {
            if (e instanceof IMRuntimeException) {
                result = ((IMRuntimeException) e).createResult();
            } else {
                logger.error("getCandyBoxDetail UN_KNOW_ERROR : {}", e.getMessage(), e);
                result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
            }
        }
        return result;
    }
}
